/*    

    bobbietalk - a visual programming environment for toy robotics

    Copyright (C) 2013  Roberto Tiella

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
package bobbietalk.gui.blocks.ifblk.open;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import bobbietalk.gui.ProgrammingBlock;
import bobbietalk.Utilities;
import bobbietalk.gui.blocks.RightBlock;
import java.awt.Color;
import java.awt.Rectangle;
import java.io.PrintWriter;

/**
 *
 * @author tiella
 */
public class IfOpenBlock extends RightBlock /* implements IfGroup */ {

    private IfOpenFalseBlock ifFalseBlock;
    private IfOpenTrueBlock ifTrueBlock;

    public IfOpenBlock() {

        setBackground(new Color(0xF0A843));
        setText(Utilities.getTextBundle().getString("menu.item.add.if"));

    }

    public void setIfFalseBlock(IfOpenFalseBlock ifFalseBlock) {
        this.ifFalseBlock = ifFalseBlock;
        ifFalseBlock.setEntryBlock(this);
    }

    public void setIfTrueBlock(IfOpenTrueBlock ifTrueBlock) {
        this.ifTrueBlock = ifTrueBlock;
        ifTrueBlock.setEntryBlock(this);
    }

    @Override
    public void setPosition(int x, int y) {

        setRPosition(x, y);

        ifFalseBlock.setRPosition(x + getWidth(), y + getHeight() * 3 / 4);
        ifTrueBlock.setRPosition(x + getWidth(), y - getHeight() * 3 / 4);

    }

    protected void setRPosition(int x, int y) {

        Rectangle r = getBounds();

        r.x = x;
        r.y = y;

        setBounds(r);
    }

    @Override
    public void setName(String name) {
        super.setName(name);
        ifFalseBlock.setName("false_" + name);
        ifTrueBlock.setName("true_" + name);
    }

    public static void moveRelative(ProgrammingBlock block, int x0, int x, int y0, int y) {

        Rectangle r = block.getBounds();

        r.x += (x - x0);
        r.y += (y - y0);

        block.setBounds(r);

    }

    public IfOpenFalseBlock getIfFalseBlock() {
        return ifFalseBlock;
    }

    public IfOpenTrueBlock getIfTrueBlock() {
        return ifTrueBlock;
    }

    void setRDeltaPosition(int x0, int x, int y0, int y) {

        moveRelative(this, x0, x, y0, y);

    }

    @Override
    public void propagateMotion(boolean moveYourself, int dx, int dy) {

        if (moveYourself) {
            moveRelative(this, 0, dx, 0, dy);
        }

        // ifFalseBlock.propagateMotion(false, dx,dy);
        ifTrueBlock.propagateMotion(false, dx, dy);

    }

    @Override
    public void dump(PrintWriter pw) {

        super.dump(pw);

        pw.println("true branch: " + ifTrueBlock);
        pw.println("false branch:" + ifFalseBlock);

    }
}
